package com.wf.course.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.utils.dateUtil.DateUtil;
import com.utils.page.PageBean;
import com.wf.course.dao.IMemberCouponsMapper;
import com.wf.course.model.ad.Coupons;
import com.wf.course.model.ad.MemberCoupons;
import com.wf.course.service.IMemberCouponsService;

@Service
@Transactional
public class IMemberCouponsServiceImpl implements IMemberCouponsService {

	@Autowired
	private IMemberCouponsMapper mapper;

	@Override
	public void queryMemberCouponsList(PageBean<MemberCoupons> pageBean) {
		// TODO Auto-generated method stub
		pageBean.setResults(mapper.queryMemberCouponsList(pageBean));
		pageBean.setTotalCount(mapper.queryMemberCouponsCounts(pageBean));
	}

	@Override
	public void getMemberCouponsList(PageBean<MemberCoupons> pageBean) {
		// TODO Auto-generated method stub
		pageBean.setResults(mapper.getMemberCouponsList(pageBean));
		pageBean.setTotalCount(mapper.getMemberCouponsCount(pageBean));
	}

	@Override
	public MemberCoupons getMemberCoupons(Integer id) {
		// TODO Auto-generated method stub
		return mapper.getMemberCoupons(id);
	}

	@Override
	public void saveMemberCoupons(MemberCoupons memberCoupons) {
		// TODO Auto-generated method stub
		mapper.saveMemberCoupons(memberCoupons);
	}

	@Override
	public String saveBatchMemberCoupons(Coupons coupons, String[] userId) {
		// TODO Auto-generated method stub
		List<MemberCoupons> list = new ArrayList<>();
		if (userId != null && userId.length > 0) {
			for (int i = 0; i < userId.length; i++) {
				if (userId[i] != null) {
					MemberCoupons memberCoupons = new MemberCoupons();
					memberCoupons.setUserId(Integer.parseInt(userId[i]));
					memberCoupons.setTitle(coupons.getTitle());
					memberCoupons.setCost(coupons.getCost());
					memberCoupons.setFtype(coupons.getFtype());
					memberCoupons.setLimits(coupons.getLimits());
					memberCoupons.setValue_ids(coupons.getValue_ids());
					memberCoupons.setBegin_date(DateUtil.getCurrentTime());
					memberCoupons.setEnd_date(DateUtil.getFetureDate(coupons.getValid_date()));
					memberCoupons.setStatus(2);
					memberCoupons.setCoupons_source(1);// 优惠券来源 1、系统发放 2、活动领取
					SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					memberCoupons.setReceive_time(df.format(new Date()));
					list.add(memberCoupons);
				}
			}
		}
		try {
			mapper.saveBatchMemberCoupons(list);
			return "1";
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "0";
	}

	@Override
	public void updateMemberCoupons(MemberCoupons memberCoupons) {
		// TODO Auto-generated method stub
		mapper.updateMemberCoupons(memberCoupons);
	}

	@Override
	public void deleteMemberCoupons(Integer id) {
		// TODO Auto-generated method stub
		mapper.deleteMemberCoupons(id);
	}

	@Override
	public boolean batchdeleteMemberCoupons(String[] ids) {
		// TODO Auto-generated method stub
		return mapper.batchdeleteMemberCoupons(ids);
	}
}
